package ai.tasks;

import ai.AIConstant;
import ai.Trackable;
import ai.PathFinding;
import ai.AIUtil;
import ai.engine.AIUnit;
import ai.engine.GroupOfUnits;
import ai.engine.Task;
import ai.engine.TaskDoer;
import ai.engine.factories.TaskFactory;
import engine.UnitAction;
import engine.data.UnitType;
import util.List;

public class TransportTask extends Task {

	private static final byte STATE_PICKINGUP = 0;
	private static final byte STATE_BOARDING = 1;
	private static final byte STATE_TRAVELLING = 2;
	private static final byte STATE_UNBOARDING = 3;
	private static final byte STATE_DONE = 4;

	private byte state;

	public TransportTask(short id, Trackable target) {
		super(id, TaskFactory.TRANSPORT_TASK, AIUnit.class, "Transport", "",
				AIConstant.PRIORITY_STRONG, target, (byte) 1, (byte) 1);
		state = STATE_PICKINGUP;
	}

	public GroupOfUnits getTarget() {
		return (GroupOfUnits) target;
	}

	public boolean isTaskSuitable(TaskDoer possibleTaskDoer) {
		AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return UnitType.getTransportCapacity(possibleUnitDoer) > 1; 
	}

	public short getFormula(TaskDoer possibleTaskDoer) {
		//AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return (short) (this.priority);
	}

	public byte getAction(TaskDoer taskDoer) {
		AIUnit unitDoer = (AIUnit) taskDoer;
		switch (state) {
		case STATE_PICKINGUP:
			if (!AIUtil.isOnSameTile(unitDoer, getTarget())) {
				return PathFinding.getNextDirection(
						PathFinding.TYPE_QUICKEST, unitDoer, getTarget());
			} else {
				state = STATE_BOARDING;
			}
		case STATE_BOARDING:
			return UnitAction.SKIP;
		case STATE_TRAVELLING:
			return UnitAction.SKIP;
		case STATE_UNBOARDING:
			return UnitAction.SKIP;
		default:
			return UnitAction.SKIP;
		}
	}

	public List getAdditionalResourcesNeeded() {
		return null; // TODO demander le bon moyen de transport en fonction des besoins (bateau, avion, terre) 
	}
	
	public boolean isObjectiveAchieved() {
		return state == STATE_DONE;
	}

}
